﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace MTGenome.Gatherer
{
    internal sealed class HttpClient
    {
        private const int BUFFER_SIZE = 4096;

        public HttpClient()
        {
            Timeout = 10000 * 6 * 10;
            UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.";
        }

        /// <summary>
        /// Timeout of requests, defaults to 10,000ms
        /// </summary>
        public int Timeout
        {
            get;
            set;
        }

        /// <summary>
        /// User agent header of request, defaults to Google Chrome
        /// </summary>
        public string UserAgent
        {
            get;
            set;
        }

        private HttpWebRequest MakeRequest(string url, Dictionary<string, string> values)
        {
            if (values != null && values.Count != 0)
            {
                StringBuilder builder = new StringBuilder(url);
                builder.Append('?');
                foreach (var item in values)
                {
                    builder.Append(item.Key);
                    builder.Append('=');
                    builder.Append(item.Value);
                    builder.Append('&');
                }
                url = builder.ToString();
            }

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = WebRequestMethods.Http.Get;
            request.UserAgent = UserAgent;
            request.Timeout = Timeout;
            return request;
        }

        public string Get(string url)
        {
            return Get(url, null);
        }

        public string Get(string url, Dictionary<string, string> values)
        {
            HttpWebRequest request = MakeRequest(url, values);
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        public void DownloadFile(string url, string path)
        {
            DownloadFile(url, path, null);
        }

        public void DownloadFile(string url, string path, Dictionary<string, string> values)
        {
            byte[] buffer = new byte[BUFFER_SIZE];
            HttpWebRequest request = MakeRequest(url, values);
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (FileStream fileStream = File.Create(path))
                    {
                        int bytesRead = 0;
                        while ((bytesRead = stream.Read(buffer, 0, BUFFER_SIZE)) > 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
        }
    }
}